package com.seawaterbt.springbootshiro.multiple;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class MultipleDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> THREAD_LOCAL = new InheritableThreadLocal<>();

    @Override
    protected Object determineCurrentLookupKey() {
        return getDataSource();
    }

    /** 设置数据源 */
    public static void setDataSource(String dataSource){
        THREAD_LOCAL.set(dataSource);
    }

    /** 取得当前数据源 */
    private String getDataSource(){
        return THREAD_LOCAL.get();
    }

    /** 清除ThreadLocal上下文数据 */
    public static void remove(){
        THREAD_LOCAL.remove();
    }
}
